<!DOCTYPE html>
<html>
<head>
	<link href="../resources/presentation.css" rel="stylesheet" type="text/css" />
	<link href="../resources/syntaxhighlighter.css" rel="stylesheet" type="text/css" />
	<script src="../resources/syntaxhighlighter.min.js" type="text/javascript"></script>

	<script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
	<script src="../../jsrender.js" type="text/javascript"></script>
	<script src="../../jquery.observable.js" type="text/javascript"></script>
	<script src="../../jquery.views.js" type="text/javascript"></script>
</head>
<body>
<div class="nav"><a href="07_observable.html">Prev</a> <a href="index.html">Index</a> <a href="07_observable3.html">Next</a></div>

<h3>JsViews: Observable collection changes</h3>

<p><button id="insertPerson">Insert person</button></p>

<!--====== Container ======-->
<table><tbody id="details"></tbody></table>

<!--====== Template ======-->
<script id="personTmpl" type="text/x-jsrender">
	<tr>
		<td data-link="firstName" ></td>
		<td>
			<input data-link="firstName" />
		</td>
		<td>
			<img class="close" src="../resources/close.png" />
		</td>
	</tr>
</script>

<!--====== Script ======-->
<script type="text/javascript">

var counter = 0,
	people = [
		{
			firstName: "Jeff"
		},
		{
			firstName: "Rebecca"
		}
	];

// Compile template
$.templates( "personTmpl", "#personTmpl" );

// Data-link details container to people, using the personTmpl template
$.link.personTmpl( "#details", people );

// Observable array change: insert
$( "#insertPerson" ).click( function() {
	$.observable( people ).insert( 0, { firstName: "NewPerson" + counter++ });
});

// Observable array change: remove
$( "#details" )
	.on( "click", ".close", function() {
		$.observable( people ).remove( $.view( this ).index, 1 );
		return false;
	});

</script>

<!--================ End of Demo Section ================-->

<h4>HTML:</h4>
<pre class="brush: xml;">
...
    &lt;td data-link="firstName">&lt;/td>
    &lt;td>
        &lt;input data-link="firstName" />
    &lt;/td>

</pre>

<h4>Script:</h4>
<pre class="brush: js;">
$.link.personTmpl( "#details", people );

// Observable array change: insert
$( "#insertPerson" ).on( "click", function() {
    $.observable( people ).insert( 0, { firstName: "NewPerson" + counter++ });
});

// Observable array change: remove
$( "#details" )
    .on( "click", ".close", function() {
        $.observable( people ).remove( $.view( this ).index, 1 );
    });

</pre>
</body>
</html>
